package com.fagp.mew.cms.controller;

import com.mew.common.entity.user.User;
import com.fagp.mew.cms.messages.PageRequest;
import com.fagp.mew.cms.service.impl.UserWebService;
import com.fagp.mew.cms.messages.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserWebService userWebService;

    @ResponseBody
    @RequestMapping("/all")
    public PageResponse getAll(PageRequest p, User user){

        PageResponse all = userWebService.getUserGold(p,user);

        return all;
    }

    @ResponseBody
    @RequestMapping("/up/{id}")
    public PageResponse up(@PathVariable("id")Long id,PageRequest p){

        try {
            return userWebService.up(id,p);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResponse(false);
        }
    }

    @ResponseBody
    @RolesAllowed({"ADMIN","USER_BAN"})
    @RequestMapping("/ban")
    public PageResponse ban(User user){

        PageResponse ban = userWebService.ban(user);

        return ban;
    }

    @ResponseBody
    @RolesAllowed({"ADMIN","USER_BAN"})
    @RequestMapping("/allban")
    public PageResponse allban(Long[] ids,Integer state){


        PageResponse ban = userWebService.allban(ids,state);

        return ban;
    }

    @ResponseBody
    @RolesAllowed({"ADMIN"})
    @RequestMapping("/add")
    public PageResponse add(MultipartFile file, Long gold) {
        try {
            userWebService.add(file,gold);
            PageResponse ban = new PageResponse();//userWebService.add(request,gold);
            return ban;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResponse(false);
        }
    }

    @RequestMapping("/data/{userId}")
    public String data(@PathVariable("userId") Long userId, Model model) throws Exception{
        model.addAttribute("userId",userId);
        return "/user/userdata";
    }

    @ResponseBody
    @RequestMapping("/info/{userid}")
    public PageResponse info(@PathVariable("userid") Long userid) throws Exception{
        return userWebService.userinfo(userid);
    }

    @ResponseBody
    @RequestMapping("/findip")
    public List<List> findip(String ip) throws Exception{
        List<List> fingip = userWebService.fingip(ip);
        return fingip;
    }


    @ResponseBody
    @RequestMapping("/retained")
    public PageResponse retainedDataCount(PageRequest pageRequest) {
        return userWebService.getRetainedData();
    }

}
